/*******************
Table 3. Robustness: Effect of EITC Generosity on Self-Employment of Single, Less-educated Households, by Uber Availability
*******************/
use "data\hslesshousehold_ubereitc2005_2019", clear

egen cbsa_yr = group(cbsa_id year)
egen st_yr_kids = group(stfips year num_ekids)
			
foreach outcome in selfemp {

		//State by Year by Number of Children (compare people of same household size in same state and year)
		reghdfe `outcome' max_total_credit2020 max_total_credit2020_uber uber_exposed [pw = hhwt] if head_hsorless == 1 & hh_married == 0, absorb(st_yr_kids cbsa_id) vce(cluster stfips) 
		
		matrix b_1`outcome' = e(b)
		matrix V_1`outcome' = e(V)
		scalar edf_r1`outcome' = e(df_r)
		scalar n_1`outcome' = e(N)
		qui sum `outcome' if e(sample) == 1 
		scalar dmean_1`outcome' = r(mean)
		
		//state and year f.e.
		reghdfe `outcome' max_total_credit2020 max_total_credit2020_uber uber_exposed [pw = hhwt] if head_hsorless == 1 & hh_married == 0, absorb(stfips year cbsa_id num_ekids) vce(cluster stfips) 
		
		matrix b_2`outcome' = e(b)
		matrix V_2`outcome' = e(V)
		scalar edf_r2`outcome' = e(df_r)
		scalar n_2`outcome' = e(N)
		qui sum `outcome' if e(sample) == 1 
		scalar dmean_2`outcome' = r(mean)
		
		//No number of child f.e.
		reghdfe `outcome' max_total_credit2020 max_total_credit2020_uber uber_exposed [pw = hhwt] if head_hsorless == 1 & hh_married == 0, absorb(st_yr cbsa_id) vce(cluster stfips) 
		matrix b_3`outcome' = e(b)
		matrix V_3`outcome' = e(V)
		scalar edf_r3`outcome' = e(df_r)
		scalar n_3`outcome' = e(N)
		qui sum `outcome' if e(sample) == 1 
		scalar dmean_3`outcome' = r(mean)
		
		//Cut out recession years
		reghdfe `outcome' max_total_credit2020 max_total_credit2020_uber uber_exposed [pw = hhwt] if head_hsorless == 1 & hh_married == 0 & year>=2010, absorb(st_yr cbsa_id num_ekids) vce(cluster stfips) 
		matrix b_4`outcome' = e(b)
		matrix V_4`outcome' = e(V)
		scalar edf_r4`outcome' = e(df_r)
		scalar n_4`outcome' = e(N)
		qui sum `outcome' if e(sample) == 1 
		scalar dmean_4`outcome' = r(mean)
		
		//Exclude rural areas 
		reghdfe `outcome' max_total_credit2020 max_total_credit2020_uber uber_exposed [pw = hhwt] if head_hsorless == 1 & hh_married == 0 & met2013 ~= 0, absorb(st_yr cbsa_id num_ekids) vce(cluster stfips) 
		matrix b_5`outcome' = e(b)
		matrix V_5`outcome' = e(V)
		scalar edf_r5`outcome' = e(df_r)
		scalar n_5`outcome' = e(N)
		qui sum `outcome' if e(sample) == 1 
		scalar dmean_5`outcome' = r(mean)
		
		//Only places that adopt Uber
		reghdfe `outcome' max_total_credit2020 max_total_credit2020_uber uber_exposed [pw = hhwt] if head_hsorless == 1 & hh_married == 0 & DateofEntry ~= ., absorb(st_yr cbsa_id num_ekids) vce(cluster stfips) 
		matrix b_6`outcome' = e(b)
		matrix V_6`outcome' = e(V)
		scalar edf_r6`outcome' = e(df_r)
		scalar n_6`outcome' = e(N)
		qui sum `outcome' if e(sample) == 1 
		scalar dmean_6`outcome' = r(mean)
		
		
		//cbsa by year
		reghdfe `outcome' max_total_credit2020 max_total_credit2020_uber uber_exposed [pw = hhwt] if head_hsorless == 1 & hh_married == 0, absorb(st_yr i.cbsa_yr num_ekids) vce(cluster stfips) 
		matrix b_7`outcome' = e(b)
		matrix V_7`outcome' = e(V)
		scalar edf_r7`outcome' = e(df_r)
		scalar n_7`outcome' = e(N)
		qui sum `outcome' if e(sample) == 1 
		scalar dmean_7`outcome' = r(mean)
		
		//Only places that adopt Uber & CBSA by year effects
		reghdfe `outcome' max_total_credit2020 max_total_credit2020_uber uber_exposed [pw = hhwt] if head_hsorless == 1 & hh_married == 0 & DateofEntry ~= ., absorb(st_yr cbsa_yr num_ekids) vce(cluster stfips) 
		matrix b_8`outcome' = e(b)
		matrix V_8`outcome' = e(V)
		scalar edf_r8`outcome' = e(df_r)
		scalar n_8`outcome' = e(N)
		qui sum `outcome' if e(sample) == 1 
		scalar dmean_8`outcome' = r(mean)
		
}

//Table
cap file close regout
file open regout using "output\tab_robust.tex", write replace
//Header
file write regout "\begin{tabular}{lcccccccc}" _n
file write regout "\toprule" _n
file write regout "\toprule" _n
file write regout "& \multicolumn{8}{c}{Self-Employment} \\" _n
file write regout "& State-by-Year- & State and  & No Number  & Exclude Recession & Exclude & Only Uber  & MSA by  & Uber Adopting Areas \\" _n
file write regout "& by-Children F.E. & Year F.E. & of Child F.E. &  Years (Pre-2010) & Non-Metro Areas & Adopting Areas & Year F.E. & and MSA by Year F.E. \\" _n
file write regout "& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8)   \\" _n
file write regout "\midrule" _n
file write regout " \\" _n
//Table
file write regout "Maximum EITC (1000s)" 
foreach outcome in 1selfemp 2selfemp 3selfemp 4selfemp 5selfemp 6selfemp 7selfemp 8selfemp {
	if "`outcome'" == "1selfemp" {
		file write regout "&"
	}
	else {
		file write regout "&" %7.4f (b_`outcome'[1,1])
		if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
			file write regout "***"
		}
		if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
			file write regout "**"
		}
		if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
			file write regout "*"
		}
	}
}
file write regout "\\" _n
foreach outcome in 1selfemp 2selfemp 3selfemp 4selfemp 5selfemp 6selfemp 7selfemp 8selfemp {
	if "`outcome'" == "1selfemp" {
		file write regout "&"
	}
	else {
		file write regout "&" "(" %5.4f ( sqrt(V_`outcome'[1,1]) ) ")"
	}
}
file write regout "\\" _n
//Interaction
file write regout "Maximum EITC (1000s)*Uber in MSA " 
foreach outcome in 1selfemp 2selfemp 3selfemp 4selfemp 5selfemp 6selfemp 7selfemp 8selfemp {
	file write regout "&" %7.4f (b_`outcome'[1,2])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
foreach outcome in 1selfemp 2selfemp 3selfemp 4selfemp 5selfemp 6selfemp 7selfemp 8selfemp {
	file write regout "&" "(" %5.4f ( sqrt(V_`outcome'[2,2]) ) ")"
}
file write regout "\\" _n
//Uber
file write regout "Uber in MSA " 
foreach outcome in 1selfemp 2selfemp 3selfemp 4selfemp 5selfemp 6selfemp /*7selfemp 8selfemp*/ {
	file write regout " &" %7.4f (b_`outcome'[1,3])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
foreach outcome in 1selfemp 2selfemp 3selfemp 4selfemp 5selfemp 6selfemp /*7selfemp 8selfemp*/ {
	file write regout "&" "(" %5.4f ( sqrt(V_`outcome'[3,3]) ) ")"
}
file write regout "\\" _n

file write regout "\\" _n
//Statistics
file write regout "Dependent Mean"
foreach outcome in  1selfemp 2selfemp 3selfemp 4selfemp 5selfemp 6selfemp 7selfemp 8selfemp {
	file write regout "&" %5.2f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in 1selfemp 2selfemp 3selfemp 4selfemp 5selfemp 6selfemp 7selfemp 8selfemp {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}
file write regout "\\" _n
file write regout "\bottomrule" _n
file write regout "\bottomrule" _n
file write regout "\end{tabular}"
file close regout
	